<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\search\RadoLocationsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Shops');
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="rado-locations-index">
    <div class="box-header with-border no-padding">
<!--        <div class="pull-left">-->
<!--            --><?php //= Html::button(Yii::t('app', 'Online Sync'), ['class' => 'btn btn-success btn-sync']) ?>
<!--        </div>-->

        <div class="search-box flex flex-end no-padding">
            <?php $form = ActiveForm::begin([
                'method' => 'get',
                'id' => 'search-form',
                'options' => ['class' => 'form-horizontal'],
            ]); ?>

            <div class='input-group input-group-sm' style='width: 300px;margin-top:5px;'>
                <?= $form->field($searchModel, 'skey',[
                    'options'=>['class'=>'input-group input-group-sm','style'=>'width: 300px;'],
                    'inputOptions' => ['placeholder' => Yii::t('app', 'Search Keyword'),'class' => 'form-control pull-right'],
                ])->label(false); ?>
                <div class="input-group-btn">
                    <button class="btn btn-default" type="submit">
                        <i class="fa fa-search"></i>
                    </button>
                </div>
            </div>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

    <div class="space10"></div>

    <div class="box box-body">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'layout' => '<div class="box-body">{items}</div>
                     <div class="box-footer">
                        {summary}
                        <div class="_pager">{pager}</div>
                     </div>',
        'columns' => [
            'id',
            'code',
            'uuid',
            'shop',
            'address',
            'role',
            [
                'label' => 'created',
                'attribute' => 'created_at',
                'format' => ['datetime', 'php:Y-m-d H:i:s'],
            ],

            [
                'class' => 'ua_backend\components\ActionColumn',

                'template' => '{statistics}',
                'buttons' => [
                    'statistics' => function($url, $model){
                        return Html::a('<span class="fs-16 fa fa-bar-chart"></span><span
            class="fs-13 pd-lr-5">心跳记录</span>', 'statistics?id='.$model->id);
                    },
                ]
            ],
        ],
    ]); ?>
    </div>
</div>

<script src="/js/jquery-2.2.3.min.js"></script>
<script>
    let socket
    let isRegister = false;
    function connect(){
        try{
            socket = new WebSocket('wss://ua_api.mediaman.com.cn:8484?id=48b16f97939724fc2e59ba933cb1600bc');
            socket.onopen = function() {
                console.log("WebSocket 连接已打开");
                isRegister = true;
            };

            socket.onmessage = function(event) {
                let msg = JSON.parse(event.data)
                console.log('收到消息：', msg);
            };

            socket.onclose = function() {
                if(!isRegister){
                    console.log('WebSocket连接已关闭，400|401错误');
                }
            };

            socket.onerror = function(error) {
                console.log('WebSocket 错误：', error);
            };
        }catch (e){
            console.error('WebSocket初始化失败:', e);
        }
    }

    // connect();
    //
    // // 点击同步按钮
    // $('.btn-sync').click(function(){
    //     console.log('click');
        // let msg = {"type":"online"}
        // let data = JSON.stringify(msg)
        // socket.send(data)
    // })

</script>
